package com.example.socketchat.model;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class ChatMessage implements Serializable {
    private static final long serialVersionUID = 1L;

    private String id;
    private String senderId;
    private String receiverId; // 可以为null表示广播消息
    private String content;
    private LocalDateTime timestamp;
    private MessageType type;
    
    public enum MessageType {
        CHAT,           // 普通聊天消息
        CONNECT,        // 连接消息
        DISCONNECT,     // 断开连接消息
        BROADCAST       // 广播消息
    }
    
    public ChatMessage(String senderId, String receiverId, String content, MessageType type) {
        this.id = UUID.randomUUID().toString();
        this.senderId = senderId;
        this.receiverId = receiverId;
        this.content = content;
        this.timestamp = LocalDateTime.now();
        this.type = type;
    }
    
    public static ChatMessage createConnectMessage(String senderId) {
        return new ChatMessage(senderId, null, senderId + " has connected", MessageType.CONNECT);
    }
    
    public static ChatMessage createDisconnectMessage(String senderId) {
        return new ChatMessage(senderId, null, senderId + " has disconnected", MessageType.DISCONNECT);
    }
    
    public static ChatMessage createBroadcastMessage(String senderId, String content) {
        return new ChatMessage(senderId, null, content, MessageType.BROADCAST);
    }
    
    public static ChatMessage createChatMessage(String senderId, String receiverId, String content) {
        return new ChatMessage(senderId, receiverId, content, MessageType.CHAT);
    }
} 